/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetradar.multibackstack.sample.provider;

import com.jetradar.multibackstack.sample.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/**
 * ItemProvider
 *
 * @since 2021-06-10
 */
public class ItemProvider extends BaseItemProvider {
    private List<String> list;
    private Context context;

    /**
     * ItemProvider
     *
     * @param stringList list
     * @param cont       context
     */
    public ItemProvider(List<String> stringList, Context cont) {
        this.list = stringList;
        this.context = cont;
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * getItem
     *
     * @param index i
     * @return Object
     */
    @Override
    public Object getItem(int index) {
        return list.get(index);
    }

    /**
     * getItemId
     *
     * @param index index
     * @return long
     */
    @Override
    public long getItemId(int index) {
        return index;
    }

    /**
     * getComponent
     *
     * @param index              index
     * @param component          component
     * @param componentContainer componentContainer
     * @return Component
     */
    @Override
    public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, null, false);
            holder = new ViewHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (ViewHolder) cpt.getTag();
        }
        holder.text.setText(list.get(index));
        return cpt;
    }

    /**
     * ViewHolder
     *
     * @since 2021-06-10
     */
    static class ViewHolder {
        private Text text;

        /**
         * ViewHolder
         *
         * @param component component
         */
        ViewHolder(Component component) {
            text = (Text) component.findComponentById(ResourceTable.Id_item_text);
        }

        /**
         * getText
         *
         * @return Text
         */
        public Text getText() {
            return text;
        }

        /**
         * setText
         *
         * @param textname textname
         */
        public void setText(Text textname) {
            this.text = textname;
        }
    }
}
